import { resolve } from 'path';
import { Module, MiddlewareConsumer, RequestMethod } from '@nestjs/common';
import { TypeOrmModule } from '@nestjs/typeorm';
import { testModule } from './modules/test/test.module';
import { ConfigModules } from './config/config.modules';
import { getConfiguration } from './config/configuration';
import { ConfigModule, ConfigService } from '@nestjs/config';
import { UsersModule } from 'src/modules/users/users.module';
import { AuthModule } from './auth/auth.module';
// import { UploadService } from './modules/upload/upload.service';
import { UploadModule } from './modules/upload/upload.module';

@Module({
  imports: [
    // 配置数据库 如环境变量、命令行参数、配置文件等
    ConfigModule.forRoot({
      isGlobal: true,
      //配置为全局可见，否则需要在每个模块中单独导入ConfigModule
      load: [getConfiguration], // 全局都可以访问到
      //导入自定义配置文件，文件靠前的优先执行
      envFilePath: [`.env.${process.env.NODE_ENV}`, '.env'],
      //配置文件路径，也可以配置为数组如['/config/.env1','.env']。
    }),
    // 连接数据库
    TypeOrmModule.forRootAsync({
      imports: [ConfigModule],
      useFactory: (configService) => ({
        autoLoadEntities: true,
        type: configService.get('database.type'),
        host: configService.get('database.host'),
        port: configService.get('database.port'),
        username: configService.get('database.username'),
        password: configService.get('database.password'),
        database: configService.get('database.database'),
        synchronize: configService.get('database.synchronize'),
        logging: configService.get('database.logging'),
        timezone: configService.get('database.timezone'), // 时区
      }),
      inject: [ConfigService],
    }),
    testModule,
    // 引入全局模块
    ConfigModules,
    UsersModule,
    AuthModule,
    UploadModule,
  ],
  // providers: [UploadService],
})
export class AppModule {}
